using System;
using System.Drawing;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows.Forms;

namespace SpellingQuiz
{
	/// <summary>
	/// Summary description for ConfigForm.
	/// </summary>
	public class ConfigForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.ListBox WordListBox;
		private System.Windows.Forms.TextBox WordTextBox;
		private System.Windows.Forms.Button NewButton;
		private System.Windows.Forms.Button AddUpdateButton;

		private SpellingUser user;
		private int selectedIndex = -1;
		private System.Windows.Forms.Button ClearListButton;
		private System.Windows.Forms.Button DeleteButton;
		private System.Windows.Forms.TextBox PhraseTextBox;
		private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private WaveLib.SoundUserControl soundUserControl1;
        private GroupBox groupBox1;
        private Button CloseButton;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public ConfigForm( )
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
            user = Utilities.GetCurrentUser();
            this.Text = "Spelling List [" + user.UserName + "]";

		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.label1 = new System.Windows.Forms.Label();
            this.WordListBox = new System.Windows.Forms.ListBox();
            this.WordTextBox = new System.Windows.Forms.TextBox();
            this.NewButton = new System.Windows.Forms.Button();
            this.AddUpdateButton = new System.Windows.Forms.Button();
            this.ClearListButton = new System.Windows.Forms.Button();
            this.DeleteButton = new System.Windows.Forms.Button();
            this.PhraseTextBox = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.soundUserControl1 = new WaveLib.SoundUserControl();
            this.CloseButton = new System.Windows.Forms.Button();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.Location = new System.Drawing.Point(8, 8);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(64, 16);
            this.label1.TabIndex = 0;
            this.label1.Text = "Word List";
            // 
            // WordListBox
            // 
            this.WordListBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.WordListBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.WordListBox.ItemHeight = 16;
            this.WordListBox.Location = new System.Drawing.Point(8, 40);
            this.WordListBox.Name = "WordListBox";
            this.WordListBox.Size = new System.Drawing.Size(226, 196);
            this.WordListBox.TabIndex = 0;
            this.WordListBox.SelectedIndexChanged += new System.EventHandler(this.WordListBox_SelectedIndexChanged);
            // 
            // WordTextBox
            // 
            this.WordTextBox.Font = new System.Drawing.Font("Arial Narrow", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.WordTextBox.Location = new System.Drawing.Point(6, 51);
            this.WordTextBox.Name = "WordTextBox";
            this.WordTextBox.Size = new System.Drawing.Size(202, 20);
            this.WordTextBox.TabIndex = 1;
            // 
            // NewButton
            // 
            this.NewButton.Font = new System.Drawing.Font("Arial Narrow", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.NewButton.Location = new System.Drawing.Point(11, 127);
            this.NewButton.Name = "NewButton";
            this.NewButton.Size = new System.Drawing.Size(53, 23);
            this.NewButton.TabIndex = 3;
            this.NewButton.Text = "New";
            this.NewButton.Click += new System.EventHandler(this.NewButton_Click);
            // 
            // AddUpdateButton
            // 
            this.AddUpdateButton.Font = new System.Drawing.Font("Arial Narrow", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.AddUpdateButton.Location = new System.Drawing.Point(73, 127);
            this.AddUpdateButton.Name = "AddUpdateButton";
            this.AddUpdateButton.Size = new System.Drawing.Size(66, 23);
            this.AddUpdateButton.TabIndex = 4;
            this.AddUpdateButton.Text = "Add/Update";
            this.AddUpdateButton.Click += new System.EventHandler(this.AddUpdateButton_Click);
            // 
            // ClearListButton
            // 
            this.ClearListButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.ClearListButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.ClearListButton.Location = new System.Drawing.Point(150, 8);
            this.ClearListButton.Name = "ClearListButton";
            this.ClearListButton.Size = new System.Drawing.Size(80, 23);
            this.ClearListButton.TabIndex = 6;
            this.ClearListButton.Text = "Clear List";
            this.ClearListButton.Click += new System.EventHandler(this.DeleteListButton_Click);
            // 
            // DeleteButton
            // 
            this.DeleteButton.Font = new System.Drawing.Font("Arial Narrow", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.DeleteButton.Location = new System.Drawing.Point(148, 127);
            this.DeleteButton.Name = "DeleteButton";
            this.DeleteButton.Size = new System.Drawing.Size(64, 23);
            this.DeleteButton.TabIndex = 5;
            this.DeleteButton.Text = "Delete";
            this.DeleteButton.Click += new System.EventHandler(this.DeleteButton_Click);
            // 
            // PhraseTextBox
            // 
            this.PhraseTextBox.Location = new System.Drawing.Point(6, 99);
            this.PhraseTextBox.Name = "PhraseTextBox";
            this.PhraseTextBox.Size = new System.Drawing.Size(202, 22);
            this.PhraseTextBox.TabIndex = 2;
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(6, 75);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(83, 16);
            this.label2.TabIndex = 8;
            this.label2.Text = "Help Phrase";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(6, 27);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(41, 16);
            this.label3.TabIndex = 9;
            this.label3.Text = "Word";
            // 
            // groupBox1
            // 
            this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.groupBox1.Controls.Add(this.label3);
            this.groupBox1.Controls.Add(this.soundUserControl1);
            this.groupBox1.Controls.Add(this.DeleteButton);
            this.groupBox1.Controls.Add(this.WordTextBox);
            this.groupBox1.Controls.Add(this.PhraseTextBox);
            this.groupBox1.Controls.Add(this.AddUpdateButton);
            this.groupBox1.Controls.Add(this.label2);
            this.groupBox1.Controls.Add(this.NewButton);
            this.groupBox1.Location = new System.Drawing.Point(12, 242);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(222, 156);
            this.groupBox1.TabIndex = 11;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Word Details";
            // 
            // soundUserControl1
            // 
            this.soundUserControl1.Font = new System.Drawing.Font("Microsoft Sans Serif", 7F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.soundUserControl1.Location = new System.Drawing.Point(53, 20);
            this.soundUserControl1.Name = "soundUserControl1";
            this.soundUserControl1.ShowProgress = true;
            this.soundUserControl1.ShowRecordButton = true;
            this.soundUserControl1.ShowStopButton = true;
            this.soundUserControl1.Size = new System.Drawing.Size(165, 26);
            this.soundUserControl1.SoundSample = new byte[0];
            this.soundUserControl1.TabIndex = 10;
            // 
            // CloseButton
            // 
            this.CloseButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)));
            this.CloseButton.Font = new System.Drawing.Font("Arial Narrow", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.CloseButton.Location = new System.Drawing.Point(84, 404);
            this.CloseButton.Name = "CloseButton";
            this.CloseButton.Size = new System.Drawing.Size(64, 23);
            this.CloseButton.TabIndex = 13;
            this.CloseButton.Text = "Close";
            this.CloseButton.Click += new System.EventHandler(this.CloseButton_Click);
            // 
            // ConfigForm
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
            this.ClientSize = new System.Drawing.Size(242, 431);
            this.ControlBox = false;
            this.Controls.Add(this.CloseButton);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.ClearListButton);
            this.Controls.Add(this.WordListBox);
            this.Controls.Add(this.label1);
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.Name = "ConfigForm";
            this.ShowInTaskbar = false;
            this.Text = "Spelling List";
            this.Load += new System.EventHandler(this.ConfigForm_Load);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void ConfigForm_Load(object sender, System.EventArgs e)
		{
			BindList();
			WordTextBox.Focus();
		}

		private void BindList()
		{
			WordListBox.DataSource = user.WordList;
			WordListBox.DisplayMember = "SpellingWord";
		}

		private void WordListBox_SelectedIndexChanged(object sender, System.EventArgs e)
		{
            selectedIndex = WordListBox.SelectedIndex;

			if( selectedIndex > -1 )
			{
                SpellingEntry item = (WordListBox.SelectedItem as SpellingEntry);

				WordTextBox.Text = item.SpellingWord;
				PhraseTextBox.Text = item.ContextPhrase;
                soundUserControl1.SoundSample = item.SoundRecordingBytes;
			}
			else
			{
				WordTextBox.Text = "";
				PhraseTextBox.Text = "";
                soundUserControl1.SoundSample = null;
			}
		}

		private void NewButton_Click(object sender, System.EventArgs e)
		{
			WordListBox.SelectedIndex = -1;

            WordTextBox.Focus();
		}

		private void AddUpdateButton_Click(object sender, System.EventArgs e)
		{
			string theWord = WordTextBox.Text.Trim().ToLower();
			string thePhrase = PhraseTextBox.Text.Trim().ToLower();
            SpellingEntry entry;

			if( theWord.Length > 0)
			{
                if (selectedIndex > -1)
                    entry = WordListBox.SelectedItem as SpellingEntry;
                else
                {
                    entry = new SpellingEntry();
                    user.WordList.Add(entry);
                }

				WordListBox.DataSource = null;
				
                entry.SpellingWord = theWord;
				entry.ContextPhrase = thePhrase;
                entry.SoundRecordingBytes = soundUserControl1.SoundSample;

				BindList();
				WordListBox.SelectedIndex = -1;
			}
			WordTextBox.Focus();
		}

		private void DeleteListButton_Click(object sender, System.EventArgs e)
		{
			if( MessageBox.Show( "Are you sure you want to delete all of the entries?", "Delete list?", MessageBoxButtons.YesNo ) == DialogResult.Yes )
			{
				WordListBox.SelectedIndex = -1;
				WordListBox.DataSource = null;
				user.WordList.Clear();
				BindList();

                WordTextBox.Text = "";
				PhraseTextBox.Text = "";
                soundUserControl1.SoundSample = null;
			}
			WordTextBox.Focus();
		}

		private void DeleteButton_Click(object sender, System.EventArgs e)
		{
			if( selectedIndex > -1 )
			{
				// Save the selectedIndex as it is about to change.
				int localSelectedIndex = selectedIndex;

				WordListBox.SelectedIndex = -1;
				WordListBox.DataSource = null;
				user.WordList.RemoveAt(localSelectedIndex);
				
                WordTextBox.Text = "";
				PhraseTextBox.Text = "";
                soundUserControl1.SoundSample = null;
				BindList();
			}
			WordTextBox.Focus();
		}

        private void CloseButton_Click(object sender, EventArgs e)
        {
            Utilities.SaveData();
            this.Close();
        }

	}
}
